package com.gzhryc.socket;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class SocketNodeCache {
	
	final ConcurrentHashMap<String,SocketNode> store;
	
	public SocketNodeCache() {
		store = new ConcurrentHashMap<String,SocketNode>();
	}

	public boolean add(SocketNode socketNode){
		if(socketNode != null){
			store.put(socketNode.getId(), socketNode);
			return true;
		}
		return false;
	}
	
	public SocketNode remove(String id) {
		return store.remove(id);
	}
	
	public SocketNode find(String id) {
		return store.get(id);
	}
	
	public List<SocketNode> getList(){
		return new ArrayList<SocketNode>(store.values());
	}
	
	public void clear() {
		for(SocketNode node : store.values()) {
			node.close();
		}
		store.clear();
	}
}
